﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using DevExpress.XtraEditors;
using DTO;
using BLL;

namespace CRM.GUI.HeThong
{
    public partial class ucTaskConfig : UserControl
    {
        public ucTaskConfig()
        {
            InitializeComponent();
            IntialLoad();
        }

        System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration
                    (System.Windows.Forms.Application.ExecutablePath);

        private void txtDuration_Leave(object sender, EventArgs e)
        {
            CheckInput(txtC1);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SaveConfig();
        }

        private void SaveConfig()
        {
            cDataInTransfer dt = new cDataInTransfer();
            int[] callsetting = new int[3];
            callsetting[0] = int.Parse(txtC1.Text);
            callsetting[1] = int.Parse(txtE1.Text);
            callsetting[2] = int.Parse(txtE2.Text);
            dt.UpdateCallsetting(callsetting);
            MessageBox.Show("Thiết lập thành công\n" + "Xin hãy khởi động lại chương trình", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void IntialLoad()
        {
            cDataInTransfer dt = new cDataInTransfer();
            dt.LoadCallSetting();
            txtC1.Text = SharedData.C2Policy;
            txtE1.Text = SharedData.E1Policy;
            txtE2.Text = SharedData.E2Policy;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            var parentform = this.ParentForm;
            parentform.Close();
        }

        private void CheckInput(TextEdit textbox)
        {
            if (textbox.Text.Length == 0)
            {
                MessageBox.Show("Không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textbox.EditValue = textbox.OldEditValue;
                textbox.Focus();
            }
            else
            {
                if (int.Parse(textbox.Text) > 365 || int.Parse(textbox.Text) == 0)
                {
                    textbox.EditValue = textbox.OldEditValue;
                }
            }
        }

        private void txtE1_Leave(object sender, EventArgs e)
        {
            CheckInput(txtE1);
        }

        private void txtE2_Leave(object sender, EventArgs e)
        {
            CheckInput(txtE2);
        }
    }
}
